sources <- c("ESPN", "FantasyData", "FantasyPros", "FantasySharks", 
             "FantasyFootballNerd", "NumberFire", "Yahoo",
             "RTSports", "Walterfootball")

scrape <- scrape_data(src = sources,
                      pos = c('QB', 'RB', 'WR', 'TE', 'DST'),
                      season = 2020, 
                      week = week)

Simulation Time!

n_sims <- 10000

tic()
sim_lu <- map_df(1:n_sims, generate_lineup) %>%
  rename(pts_base = points) %>%  
  mutate(position = factor(position, 
                           levels = c("QB", "RB", "WR", "TE", "DST"))) %>% 
  select(lineup, Name, team, position, pts_base, pts_pred, sd_pts, Salary)
toc()
## 291.066 sec elapsed

Results

ggplotly(sim_lu %>% 
           group_by(Name, position, Salary) %>% 
           dplyr::summarize(lu = n_distinct(lineup)) %>% 
           ungroup() %>% 
           group_by(position) %>% 
           top_n(10, lu) %>% 
           ungroup() %>% 
           arrange(position, desc(lu)) %>% 
           mutate(Name = factor(Name),
                  Name = fct_reorder(Name, lu),
                  pct = round(lu / n_sims, 3) * 100) %>% 
           ggplot(aes(x = Name, y = pct, fill = Salary,
                      text = paste(Name, "in", lu, "lineups with", Salary, "salary"))) +
           geom_bar(stat = "identity") +
           facet_wrap(~position, ncol = 2, scales = "free_y") +
           coord_flip() +
           scale_fill_viridis_c() +
           xlab("") +
           ylab("Lineups (thousands)") +
           ggtitle("Top 10 Players By Position")) %>% 
  ggplotly(tooltip = "text")
plyr_lu <- sim_lu %>%
  group_by(Name, position) %>%
  dplyr::summarize(lu=n_distinct(lineup)) %>%
  ungroup() 

ggplotly(projections %>% 
  filter(avg_type=='weighted') %>%
  mutate(Name = ifelse(pos=="DST", last_name, paste(first_name, last_name))) %>%
  inner_join(fan_duel, by = c("Name", "position")) %>%
  select(Name, team, position, points, Salary, sd_pts) %>%
  left_join(plyr_lu, by='Name') %>%
  replace_na(list(lu=0)) %>%
  mutate(lu_bin=ifelse(lu==0, '0 Lineups', '>=1 Lineups'),
         lu_5=cut(lu,5, labels = FALSE)) %>%
  ggplot(aes(x=Salary, y=points, color=lu_bin, size=sd_pts, text=Name)) +
  geom_point() +
  theme_minimal() +
  scale_color_manual(values = c('red', 'blue'), name="") +
  geom_smooth(inherit.aes = FALSE, aes(x=Salary, y=points), method = 'lm') +
  ylab('Projected Points') +
  xlab('Salary') +
  ggtitle('Who makes it into Optimized Lineups?') +
  scale_x_continuous(labels=scales::dollar))
lu_df <- sim_lu %>%
  group_by(lineup) %>%
  dplyr::summarize(lineup_pts=sum(pts_pred),
                   lineup_sd=sum(sd_pts)) %>%
  ungroup()

pto <- psel(lu_df, low(lineup_sd) * high(lineup_pts))


ggplotly(ggplot(lu_df, aes(y=lineup_pts, x=lineup_sd, text = paste0("Lineup: ", lineup))) +
  geom_point() +
  geom_point(data=pto, size=5) +
  ylab('Lineup Points') +
  xlab('Lineup Points St Dev') +
  ggtitle('Lineup Points vs. Uncertainty',
          subtitle = 'Pareto Lineups Bolded'))
psel(lu_df, low(lineup_sd) * high(lineup_pts)) %>%
  left_join(sim_lu, by='lineup') %>%
  group_by(lineup) %>%
  arrange(lineup_pts, position, desc(Salary)) %>%
  select(lineup, lineup_pts, lineup_sd, Name, team, position, pts_pred, sd_pts, Salary) %>%
  mutate_at(vars(lineup_pts, lineup_sd, pts_pred, sd_pts), function(x) round(x, 2)) %>%
  knitr::kable() %>%
  kable_styling(fixed_thead = T) %>%
  column_spec(1:3, bold=TRUE) %>%
  collapse_rows(columns = 1:3, valign = 'top') %>%
  scroll_box(height = '700px', width = '100%')
lineup lineup_pts lineup_sd Name team position pts_pred sd_pts Salary
3875 128.84 4.93 Deshaun Watson HOU QB 23.38 0.17 8300
James Robinson JAC RB 16.88 0.91 7300
David Montgomery CHI RB 14.26 0.98 5900
DeeJay Dallas SEA RB 11.95 0.76 5100
DeAndre Hopkins ARI WR 16.41 0.54 8800
Julio Jones ATL WR 15.78 0.68 8200
Stefon Diggs BUF WR 15.56 0.29 7600
Logan Thomas WAS TE 7.94 0.48 5000
Houston Texans HOU DST 6.68 0.11 3700
827 131.12 5.05 Deshaun Watson HOU QB 23.34 0.17 8300
Dalvin Cook MIN RB 20.61 1.34 9300
James Robinson JAC RB 18.14 0.91 7300
DeeJay Dallas SEA RB 14.01 0.76 5100
Julio Jones ATL WR 17.02 0.68 8200
Stefon Diggs BUF WR 15.32 0.29 7600
Cole Beasley BUF WR 9.91 0.05 5300
Logan Thomas WAS TE 7.83 0.48 5000
Dallas Cowboys DAL DST 4.95 0.36 3000
7420 131.59 5.38 Deshaun Watson HOU QB 23.40 0.17 8300
Derrick Henry TEN RB 19.28 0.91 8900
David Montgomery CHI RB 14.06 0.98 5900
DeeJay Dallas SEA RB 12.84 0.76 5100
Julio Jones ATL WR 15.84 0.68 8200
Stefon Diggs BUF WR 15.51 0.29 7600
Cole Beasley BUF WR 10.01 0.05 5300
Evan Engram NYG TE 9.33 0.63 5500
Pittsburgh Steelers PIT DST 11.31 0.90 5000
915 133.44 5.39 Deshaun Watson HOU QB 23.33 0.17 8300
Dalvin Cook MIN RB 20.19 1.34 9300
James Robinson JAC RB 18.80 0.91 7300
DeeJay Dallas SEA RB 13.54 0.76 5100
Stefon Diggs BUF WR 15.68 0.29 7600
Keenan Allen LAC WR 15.08 0.90 7500
Cole Beasley BUF WR 9.87 0.05 5300
Darren Waller LVR TE 11.61 0.61 6400
Dallas Cowboys DAL DST 5.35 0.36 3000
9096 135.35 5.49 Deshaun Watson HOU QB 23.28 0.17 8300
Dalvin Cook MIN RB 22.28 1.34 9300
David Montgomery CHI RB 14.69 0.98 5900
DeeJay Dallas SEA RB 13.09 0.76 5100
DeAndre Hopkins ARI WR 17.01 0.54 8800
Stefon Diggs BUF WR 15.18 0.29 7600
Cole Beasley BUF WR 9.96 0.05 5300
Darren Waller LVR TE 12.44 0.61 6400
Atlanta Falcons ATL DST 7.42 0.74 3300
9332 137.90 6.10 Deshaun Watson HOU QB 23.67 0.17 8300
Dalvin Cook MIN RB 20.91 1.34 9300
Chase Edmonds ARI RB 20.26 1.66 6700
DeeJay Dallas SEA RB 13.54 0.76 5100
Stefon Diggs BUF WR 15.85 0.29 7600
Brandin Cooks HOU WR 11.09 0.31 6100
Cole Beasley BUF WR 9.82 0.05 5300
Darren Waller LVR TE 13.49 0.61 6400
Pittsburgh Steelers PIT DST 9.27 0.90 5000
3974 137.95 6.30 Josh Allen BUF QB 24.23 0.69 8200
Dalvin Cook MIN RB 23.92 1.34 9300
James Robinson JAC RB 17.34 0.91 7300
DeeJay Dallas SEA RB 12.75 0.76 5100
Stefon Diggs BUF WR 15.57 0.29 7600
Tyler Lockett SEA WR 14.96 0.90 7400
Cole Beasley BUF WR 9.88 0.05 5300
Darren Waller LVR TE 11.97 0.61 6400
Atlanta Falcons ATL DST 7.33 0.74 3300
8512 139.73 6.80 Deshaun Watson HOU QB 23.46 0.17 8300
Josh Jacobs LVR RB 18.39 0.91 7700
Chase Edmonds ARI RB 17.87 1.66 6700
DeeJay Dallas SEA RB 12.26 0.76 5100
Julio Jones ATL WR 17.05 0.68 8200
Stefon Diggs BUF WR 15.71 0.29 7600
Keenan Allen LAC WR 16.52 0.90 7500
Hayden Hurst ATL TE 10.04 0.69 5600
Atlanta Falcons ATL DST 8.44 0.74 3300
3172 139.76 7.18 Deshaun Watson HOU QB 23.48 0.17 8300
Dalvin Cook MIN RB 22.31 1.34 9300
Antonio Gibson WAS RB 20.87 2.54 6200
DeeJay Dallas SEA RB 13.00 0.76 5100
Julio Jones ATL WR 16.52 0.68 8200
Stefon Diggs BUF WR 15.41 0.29 7600
Cole Beasley BUF WR 9.84 0.05 5300
Darren Waller LVR TE 11.45 0.61 6400
Atlanta Falcons ATL DST 6.88 0.74 3300
6653 140.27 7.29 Deshaun Watson HOU QB 23.36 0.17 8300
James Robinson JAC RB 17.15 0.91 7300
Chase Edmonds ARI RB 18.99 1.66 6700
DeeJay Dallas SEA RB 13.11 0.76 5100
Julio Jones ATL WR 16.86 0.68 8200
Stefon Diggs BUF WR 16.05 0.29 7600
DeVante Parker MIA WR 13.77 1.29 6300
Evan Engram NYG TE 10.58 0.63 5500
Pittsburgh Steelers PIT DST 10.41 0.90 5000
5496 141.49 7.47 Deshaun Watson HOU QB 23.40 0.17 8300
Derrick Henry TEN RB 20.85 0.91 8900
Antonio Gibson WAS RB 21.84 2.54 6200
DeeJay Dallas SEA RB 13.65 0.76 5100
Julio Jones ATL WR 16.19 0.68 8200
Stefon Diggs BUF WR 15.48 0.29 7600
Robby Anderson CAR WR 14.14 0.75 6600
Evan Engram NYG TE 9.19 0.63 5500
Atlanta Falcons ATL DST 6.77 0.74 3300
9378 142.68 7.59 Kyler Murray ARI QB 24.14 0.55 8600
James Robinson JAC RB 17.62 0.91 7300
Antonio Gibson WAS RB 22.70 2.54 6200
DeeJay Dallas SEA RB 12.83 0.76 5100
Julio Jones ATL WR 16.75 0.68 8200
Stefon Diggs BUF WR 15.34 0.29 7600
Keenan Allen LAC WR 16.19 0.90 7500
Darren Waller LVR TE 11.34 0.61 6400
Dallas Cowboys DAL DST 5.77 0.36 3000
9882 144.28 7.60 Deshaun Watson HOU QB 23.47 0.17 8300
James Robinson JAC RB 17.42 0.91 7300
Antonio Gibson WAS RB 24.14 2.54 6200
DeeJay Dallas SEA RB 12.89 0.76 5100
Julio Jones ATL WR 15.82 0.68 8200
Stefon Diggs BUF WR 15.71 0.29 7600
Keenan Allen LAC WR 16.42 0.90 7500
Darren Waller LVR TE 11.70 0.61 6400
Atlanta Falcons ATL DST 6.70 0.74 3300
6415 144.85 8.64 Deshaun Watson HOU QB 23.21 0.17 8300
James Robinson JAC RB 17.69 0.91 7300
Antonio Gibson WAS RB 20.06 2.54 6200
DeeJay Dallas SEA RB 12.72 0.76 5100
Stefon Diggs BUF WR 15.63 0.29 7600
Tyler Lockett SEA WR 15.48 0.90 7400
Justin Jefferson MIN WR 16.00 1.32 6800
Travis Kelce KCC TE 15.61 1.02 8000
Atlanta Falcons ATL DST 8.44 0.74 3300
3499 144.97 8.86 Deshaun Watson HOU QB 23.26 0.17 8300
Dalvin Cook MIN RB 21.12 1.34 9300
Antonio Gibson WAS RB 19.45 2.54 6200
DeeJay Dallas SEA RB 14.02 0.76 5100
Stefon Diggs BUF WR 15.97 0.29 7600
Keenan Allen LAC WR 17.87 0.90 7500
Terry McLaurin WAS WR 15.15 1.48 7200
Evan Engram NYG TE 10.24 0.63 5500
Atlanta Falcons ATL DST 7.89 0.74 3300
3766 145.95 8.88 Josh Allen BUF QB 23.98 0.69 8200
James Robinson JAC RB 17.59 0.91 7300
Chase Edmonds ARI RB 19.38 1.66 6700
Antonio Gibson WAS RB 22.92 2.54 6200
Julio Jones ATL WR 16.17 0.68 8200
Stefon Diggs BUF WR 15.21 0.29 7600
Tyler Lockett SEA WR 15.02 0.90 7400
Logan Thomas WAS TE 6.76 0.48 5000
Atlanta Falcons ATL DST 8.92 0.74 3300
4096 145.98 11.41 Deshaun Watson HOU QB 23.25 0.17 8300
Dalvin Cook MIN RB 19.85 1.34 9300
Jonathan Taylor IND RB 16.48 2.28 6400
Antonio Gibson WAS RB 21.60 2.54 6200
Julio Jones ATL WR 16.38 0.68 8200
Keenan Allen LAC WR 17.01 0.90 7500
Keelan Cole JAC WR 15.12 2.07 5200
Hayden Hurst ATL TE 9.25 0.69 5600
Atlanta Falcons ATL DST 7.04 0.74 3300
1067 152.06 11.70 Kyler Murray ARI QB 23.96 0.55 8600
Josh Jacobs LVR RB 16.57 0.91 7700
Chase Edmonds ARI RB 17.65 1.66 6700
Antonio Gibson WAS RB 23.38 2.54 6200
DeAndre Hopkins ARI WR 17.13 0.54 8800
Stefon Diggs BUF WR 15.40 0.29 7600
Marvin Hall DET WR 17.88 3.87 4600
Darren Waller LVR TE 12.92 0.61 6400
Atlanta Falcons ATL DST 7.17 0.74 3300
3589 152.17 15.45 Chase Daniel DET QB 37.99 9.39 6000
Derrick Henry TEN RB 19.31 0.91 8900
Josh Jacobs LVR RB 16.15 0.91 7700
DeeJay Dallas SEA RB 12.48 0.76 5100
Stefon Diggs BUF WR 16.06 0.29 7600
Keenan Allen LAC WR 17.19 0.90 7500
Robby Anderson CAR WR 12.80 0.75 6600
Evan Engram NYG TE 9.68 0.63 5500
Pittsburgh Steelers PIT DST 10.51 0.90 5000
1186 158.54 16.67 Chase Daniel DET QB 44.16 9.39 6000
Dalvin Cook MIN RB 20.72 1.34 9300
Josh Jacobs LVR RB 16.68 0.91 7700
James Robinson JAC RB 16.50 0.91 7300
Stefon Diggs BUF WR 15.38 0.29 7600
Justin Jefferson MIN WR 15.18 1.32 6800
Darius Slayton NYG WR 11.96 0.67 5900
T.J. Hockenson DET TE 10.67 1.09 6000
Atlanta Falcons ATL DST 7.30 0.74 3300
9594 158.74 18.20 Chase Daniel DET QB 42.92 9.39 6000
Dalvin Cook MIN RB 20.58 1.34 9300
Derrick Henry TEN RB 18.31 0.91 8900
Antonio Gibson WAS RB 19.75 2.54 6200
Keenan Allen LAC WR 16.65 0.90 7500
Cole Beasley BUF WR 9.88 0.05 5300
Nelson Agholor LVR WR 9.45 1.55 5000
Darren Waller LVR TE 11.96 0.61 6400
Pittsburgh Steelers PIT DST 9.25 0.90 5000